﻿using BeerScanatronClient.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BeerScanatronServer
{
    public partial class ScanatronDbContext : DbContext
    {
        public event CreateDatabaseDelegate CreatingDatabase;

        public delegate void CreateDatabaseDelegate(DBCreateEventArgs args);

        /// <summary>
        /// Initializes a new instance of the <see cref="ScanatronDbContext"/> class.
        /// </summary>
        /// <param name="connectionString">The connection string.</param>
        public ScanatronDbContext(string connectionString)
            : base(connectionString)
        {
        }

        /// <summary>
        /// Gets or sets the beers.
        /// </summary>
        /// <value>
        /// The beers.
        /// </value>
        public DbSet<Beer> Beers { get; set; }

        /// <summary>
        /// Gets or sets the dispenser infoes.
        /// </summary>
        /// <value>
        /// The dispenser infoes.
        /// </value>
        public DbSet<DispenserInfo> DispenserInfoes { get; set; }

        /// <summary>
        /// Gets or sets the drinkers.
        /// </summary>
        /// <value>
        /// The drinkers.
        /// </value>
        public DbSet<Drinker> Drinkers { get; set; }

        /// <summary>
        /// Gets or sets the drinker pictures.
        /// </summary>
        /// <value>
        /// The drinker pictures.
        /// </value>
        public DbSet<DrinkerPicture> DrinkerPictures { get; set; }

        /// <summary>
        /// Gets or sets the settings.
        /// </summary>
        /// <value>
        /// The settings.
        /// </value>
        public DbSet<Setting> Settings { get; set; }

        /// <summary>
        /// Called when [model creating].
        /// </summary>
        /// <param name="modelBuilder">The model builder.</param>
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            if (!this.OnCreatingDatabase())
            {
                Database.SetInitializer<ScanatronDbContext>(null);
            }
        }

        /// <summary>
        /// Called when [creating database].
        /// </summary>
        private bool OnCreatingDatabase()
        {
            if (CreatingDatabase != null)
            {
                DBCreateEventArgs createArgs = new DBCreateEventArgs();
                CreatingDatabase(createArgs);
                if (!createArgs.AllowDatabaseCreation)
                {
                    return false;
                }
            }

            return true;
        }
    }

    public class DBCreateEventArgs : EventArgs
    {
        public bool AllowDatabaseCreation { get; set; }
    }
}